package server;
import server.Data;

import java.rmi.RemoteException;
import java.sql.*;

public class SaleCom extends Company implements SaleComInterface
{
	private Data data;
	/*
	private double maxMarketResFounds;

	
	public SaleCom(int ID, String name, int locationNum, int bankAccountID,
			double founds, int maxStaff, double maxLoanAmount, double maxMarketResFounds) {
		super(ID, name, locationNum, bankAccountID, founds, maxStaff, maxLoanAmount);
		this.maxMarketResFounds=maxMarketResFounds;

	}
	*/
	public SaleCom()throws RemoteException
	{
		data=new Data();
	}
	

	public double getMaxMaxketResFounds(int id) throws RemoteException
	{
		String sql;
		ResultSet rs=null;
		double maxMarketResFounds=0;
		
		sql="select * from company where id=" + id;
		rs=data.getResultSet(sql);
		try {
			while(rs.next()){
				maxMarketResFounds=rs.getInt("maxMarketResFounds");
		}
			} catch (SQLException e) {
			e.printStackTrace();
			}
			
		//return maxMarketResFounds;
		return maxMarketResFounds;

	}
	
	public boolean marketRes(int id,double marketResFounds) throws RemoteException
	{
		if(marketResFounds<this.getFounds(id))
		{
			if(marketResFounds<=getMaxMaxketResFounds(id))
			{
				if(Math.random()<0.5)
				{
					//��ʾ�����г�����Ч��ϲ����ҵ������
					//��˾������(0.5-Math��random())*marketResFounds
					double iar=(0.5-Math.random())*marketResFounds;
					this.setFounds(id,this.getFounds(id)-iar);
					return true;
				}
				else
				{
					//��ʾ�����̳�����Ч��Ϻã�����ҵ�����
					//��˾ӯ��(Math��random()-0.5)*marketResFounds
					double iar=(Math.random()-0.5)*marketResFounds;
					this.setFounds(id,this.getFounds(id)+iar);
					return false;
				}
			}
			else
			{
				//��ʾ�г������ʽ𳬳����
				return false;
			}
			
		}
		else
		{
			//��ʾ��˾�ʽ��㣬��ʾʣ���ʽ�
			return false;
			
		}
	}

}